package com.tanhua.backmanage.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tanhua.backmanage.pojo.Manager;
import com.tanhua.backmanage.service.ManagerInfoService;
import com.tanhua.backmanage.service.ManagerService;
import com.tanhua.backmanage.service.VerificationService;
import com.tanhua.backmanage.utils.ManagerThreadLocal;
import com.tanhua.backmanage.vo.ErrorResult;
import com.tanhua.backmanage.vo.ManagerInfo;
import com.tanhua.common.utils.NoAuthorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
@Slf4j
public class ManagerController {
    @Autowired
    private ManagerInfoService managerInfoService;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private VerificationService verificationService;

    /**
     * 获取图片验证码
     *
     * @param uuid
     * @return
     */
    @NoAuthorization
    @GetMapping("/verification")
    public void getCaptcha(String uuid,HttpServletResponse response)throws Exception {
        // 生成验证码图片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);

        String code = lineCaptcha.getCode();

        log.info("生成验证码，uuid = " + uuid + ", code = " + code);
        // 将验证码的值存储到redis中
        verificationService.saveCap(uuid, code);
        // 输出验证码图片
        lineCaptcha.write(response.getOutputStream());

    }

    /**
     * 登录
     *
     * @param map
     * @return
     */
    @NoAuthorization
    @PostMapping("/login")
    public ResponseEntity<Object> login(@RequestBody Map<String, String> map) {
        //获取手机号和验证码
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        //业务层处理
        String token = managerService.login(username, password, verificationCode, uuid);


        if (token != null) {
            return ResponseEntity.ok(token);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("登录失败！").build();
        return ResponseEntity.status(500).body(errorResult);
    }

    /**
     * 校验token，根据token查询用户数据
     *
     * @param token
     * @return
     */
    @GetMapping("{token}")
    public Manager queryUserByToken(@PathVariable("token") String token) {
        return this.managerService.queryManagerByToken(token);
    }


    /**
     * 获取用户信息
     *
     * @return
     */
    @PostMapping("profile")
    public ResponseEntity<ManagerInfo> selectManagerInfo() {
        try {
            ManagerInfo managerInfo = managerInfoService.selectManagerInfo();
            if (null != managerInfo) {
                System.out.println("查询信息成功");
                return ResponseEntity.ok(managerInfo);
            }
        } catch (Exception e) {
            System.out.println("查询错误");
            log.error("查询用户信息失败 userId = " + ManagerThreadLocal.get().getId(), e);
        }
        System.out.println("查询信息失败");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }




    @Value("${tanhua.mamnagement.logouturl}")
    private String logoutUrl;

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("logout")
    public String logOut() {
        return logoutUrl;
    }
}
